package com.example.hearthstoneagain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class DeckActivity extends ActionBarActivity {

	ExpandableListAdapter adapter;
	
	ExpandableListView explst;
	
	List<String> listhd;
	
	HashMap<String, List<String>> listin;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_deck);

        Bundle extras = getIntent().getExtras();
		
		String deck = extras.getString("deck");
		
		// get the listview
        explst = (ExpandableListView) findViewById(R.id.expandableListView1);
        
        TextView edtTxt = (TextView) findViewById(R.id.textView1);
        
        edtTxt.setText(deck);
		
		// arruma o conteudo pesquisando no bd
		
		listadeconteudos(deck);
		
		
		adapter = new ExpandableListAdapter(this, listhd, listin);
		
		explst.setAdapter(adapter);
		
		explst.setOnChildClickListener(new OnChildClickListener() {
			 
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                String carta = listin.get(listhd.get(groupPosition)).get(childPosition);
                String card[] = carta.split(" ",2);
                carta = card[1];
                
                DeckTabGroupActivity group = (DeckTabGroupActivity) getParent();
                MainActivity mainact = (MainActivity) group.getParent();
                
                MainBuscaActivity.instancia.abreCarta(carta);
                
                mainact.switchTab(0);
                return false;
            }
        });
		
	}
	
	private void listadeconteudos (String deck) {
		
		listhd = new ArrayList<String>();
        listin = new HashMap<String, List<String>>();
		
		BancodeDados banco = new BancodeDados(this.getApplicationContext());
		
		SQLiteDatabase dtb = banco.getReadableDatabase();
		
		String nomecol[] = {"cartas","comentario"};
		
		String args[] = {deck};
		
		Cursor resp_deck = dtb.query("decks", nomecol, "name = ?", args, null, null, null);
		
		resp_deck.moveToFirst();
		
		String cartas = resp_deck.getString(0);
		
		String coment = resp_deck.getString(1);
		
		String carta[] = cartas.split(",");
		
		listhd.add("How to Play");
		
		listhd.add("Cards");
		
		List<String> howto = new ArrayList<String>();
		howto.add(coment);
		
		List<String> cards = new ArrayList<String>();
		for(String card : carta) {
			cards.add(card);
		}
		
		listin.put(listhd.get(0), howto);
		listin.put(listhd.get(1), cards);
		
	}
	

}
